package xyz.thoughtset.viewer.apis.client.controller;


import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.thoughtset.viewer.common.api.controller.BaseController;
import xyz.thoughtset.viewer.common.connector.linker.LinkerHelper;
import xyz.thoughtset.viewer.common.envvar.service.EnvVarsService;
import xyz.thoughtset.viewer.modules.ds.core.entity.LinkerConfig;
import xyz.thoughtset.viewer.modules.ds.core.service.LinkerConfigService;
import xyz.thoughtset.viewer.executor.core.constants.TypeConstants;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequiredArgsConstructor
public class DataTypeApi extends BaseController {
    private final LinkerConfigService linkerConfigService;
    private final EnvVarsService envVarsService;

    @GetMapping("/dataType")
    public Object dataType(){
        return TypeConstants.BASE_TYPE_LIST;
    }

    @GetMapping("/linkers")
    public Object linkers(){
        List<LinkerConfig> linkerConfigs = linkerConfigService.list();
//        return Arrays.asList("mysql5","mysql8","pgsql");
//        return LinkerHelper.getConnectorKeys();
        return Stream.concat(
                LinkerHelper.getConnectorKeys().stream(),
                linkerConfigs.stream().map(LinkerConfig::getTitle)
        ).distinct().collect(Collectors.toList());
    }

    @GetMapping("/envsWithTopic")
    public Object envsWithTopic(@RequestParam String topic){
        return envVarsService.getObjListWithTopic(topic);
    }

    @GetMapping("/getEnvVars")
    public Object getEnvVars(@RequestParam String varId){
        return envVarsService.selectDetail(varId);
    }

}
